package com.wemew.common.redis.impl;

import com.wemew.common.enums.password.PassWordEnum;
import com.wemew.common.redis.UserRedis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;
@Component
public class UserRedisImpl implements UserRedis {
    @Autowired
    private RedisTemplate redisTemplate;
    private static String TOKENCODE="user:token:code:";
    private static String TOKENPERMISSIONS="user:token:permissions:";

    @Override
    public void addTokenCode(String userName, String code) {
        String key = TOKENCODE + userName;
        redisTemplate.opsForValue().set(key, code, PassWordEnum.TOKENCODE过期.getParam(), TimeUnit.DAYS);
    }

    @Override
    public void delTokenCode(String userName) {
        String key = TOKENCODE + userName;
        redisTemplate.delete(key);
    }

    @Override
    public String getTokenCode(String userName) {
        String key = TOKENCODE + userName;
        String code = (String) redisTemplate.opsForValue().get(key);
        return code;
    }

    @Override
    public void refreshTokenCode(String userName) {
        String key = TOKENCODE + userName;
        Long param = PassWordEnum.TOKENCODE过期.getParam();
        redisTemplate.expire(key, param, TimeUnit.DAYS);
    }

    @Override
    public void addPermissions(String userName, List<String> permissions) {
        String key = TOKENPERMISSIONS + userName;
        if (permissions!=null&&!permissions.isEmpty()) {
            redisTemplate.opsForValue().set(key, permissions, PassWordEnum.TOKEN过期.getParam(), TimeUnit.DAYS);
        }
    }

    @Override
    public void delPermissions(String userName) {
        String key = TOKENPERMISSIONS + userName;
        redisTemplate.delete(key);
    }

    @Override
    public List<String> getPermissions(String userName) {
        String key = TOKENPERMISSIONS + userName;
        List<String> permissionValueList = (List<String>)redisTemplate.opsForValue().get(key);
        return permissionValueList;
    }
}
